//
//  CSGXCommPreviewCell.m
//

#import "CSGXCommPreviewCell.h"

@interface CSGXCommPreviewCell ()

@property (nonatomic,weak) UILabel *lblName;
@property (nonatomic,weak) UILabel *lblIndex;
@property (nonatomic,weak) UILabel *lblProductDate;
@property (nonatomic,weak) UILabel *lblCount;
@property (nonatomic,weak) UILabel *lblPrice;

@end

@implementation CSGXCommPreviewCell


#pragma mark - methods

- (void)distribute {
    //
#pragma mark ViewControllerCarSale
    if (self.controllerEnum == ViewControllerCarSale) {
        NSString *unitCountStr = @"";
        
        NSArray *unitArr = [self.saleCommPreviewModel.sUnit componentsSeparatedByString:@","];
        NSArray *countArr = [self.saleCommPreviewModel.sCount componentsSeparatedByString:@","];
        NSArray *priceArr = [self.saleCommPreviewModel.sPrice componentsSeparatedByString:@","];
        
        for (int i = 0; i < unitArr.count; i++) {
            unitCountStr = [unitCountStr stringByAppendingFormat:@"%@%@", countArr[i], unitArr[i]];
        }
        
        self.lblName.text = [NSString stringWithFormat:@"       %@", self.saleCommPreviewModel.sName];
        self.lblIndex.text = [NSString stringWithFormat:@"%ld", [self.dataPreviewDict[@"rowIndex"] integerValue] + 1];
        self.lblProductDate.text = [CSGXDateFormatterManager getDayWithDate:[[CSGXDateFormatterManager sharedManager] dateFromString:self.saleCommPreviewModel.sCreateTime]];
        self.lblCount.text = unitCountStr;
        self.lblPrice.text = [NSString stringWithFormat:@"%@/%@", priceArr[0], unitArr[0]];
    }
#pragma mark ViewControllerInventoryCheck
    else if (self.controllerEnum == ViewControllerInventoryCheck) {
        self.lblPrice  = nil;
        if (!self.inventoryPreviewModel) {
            NSLog(@"inventoryPreviewModel 为空!");
            return;
        }
        
        NSString *unitCountStr = @"";
        
        NSArray *unitArr = [self.inventoryPreviewModel.iUnit componentsSeparatedByString:@","];
        NSArray *countArr = [self.inventoryPreviewModel.iInventoryCount componentsSeparatedByString:@","];
        
        for (int i = 0; i < unitArr.count; i++) {
            unitCountStr = [unitCountStr stringByAppendingFormat:@"%@%@", countArr[i], unitArr[i]];
        }
        
        self.lblName.text = [NSString stringWithFormat:@"       %@", self.inventoryPreviewModel.iName];
        self.lblIndex.text = [NSString stringWithFormat:@"%ld", [self.dataPreviewDict[@"rowIndex"] integerValue] + 1];
        self.lblProductDate.text = [CSGXDateFormatterManager getDayWithDate:[[CSGXDateFormatterManager sharedManager] dateFromString:self.saleCommPreviewModel.sCreateTime]];
        self.lblCount.text = unitCountStr;
        
        self.lblCount.textColor = [UIColor redColor];
    }
}



#pragma mark - setupUI
- (void)setupUI {
    
    //lblName
    UILabel *lblName = [UILabel gx_labelWithText:@"" fontSize:12 color:[UIColor blackColor]];
    [self.contentView addSubview:lblName];
    
    [lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.leading.equalTo(self.contentView).offset(5);
        make.trailing.equalTo(self.contentView).offset(-5);
    }];
    
    //lblIndex
    UILabel *lblIndex = [UILabel gx_labelWithText:@"0" fontSize:12 color:[UIColor whiteColor]];
    [self.contentView addSubview:lblIndex];
    [self.contentView bringSubviewToFront:lblIndex];
    
    lblIndex.backgroundColor = [UIColor orangeColor];
    lblIndex.textAlignment = NSTextAlignmentCenter;
    
    [lblIndex mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.leading.equalTo(self.contentView).offset(5);
        make.size.mas_offset(CGSizeMake(18, 15));
    }];
    
    //lblProductDate
    UILabel *lblProductDate = [UILabel gx_labelWithText:@"" fontSize:12 color:[UIColor grayColor]];
    [self.contentView addSubview:lblProductDate];
    
    [lblProductDate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lblName.mas_bottom).offset(5);
        make.leading.equalTo(lblName);
    }];
    
    //lblCount
    UILabel *lblCount = [UILabel gx_labelWithText:@"" fontSize:12 color:kColorGrassGreen]; //草绿色
    [self.contentView addSubview:lblCount];
    
    [lblCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lblProductDate.mas_bottom).offset(3);
        make.leading.equalTo(lblName);
    }];
    
    //lblPrice
    UILabel *lblPrice = [UILabel gx_labelWithText:@"" fontSize:12 color:[UIColor grayColor]];
    [self.contentView addSubview:lblPrice];
    self.lblPrice = lblPrice;
    
    [lblPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lblCount.mas_bottom);
        make.leading.equalTo(lblName);
        make.bottom.equalTo(self.contentView).offset(-3);
    }];
    
    
    [self layoutIfNeeded];
    //
    self.lblName = lblName;
    self.lblIndex = lblIndex;
    self.lblProductDate = lblProductDate;
    self.lblCount = lblCount;
}




#pragma mark - setter & getter
- (void)setDataPreviewDict:(NSDictionary *)dataPreviewDict {
    _dataPreviewDict = dataPreviewDict;
    if (self.controllerEnum == ViewControllerCarSale) {
        self.saleCommPreviewModel = dataPreviewDict[@"previewModel"];
    }else if (self.controllerEnum == ViewControllerInventoryCheck) {
        self.inventoryPreviewModel = dataPreviewDict[@"previewModel"];
    }
    [self distribute];
}




@end
